package son.nt.act2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import son.nt.R;
import son.nt.common.Contains;
import son.nt.common.SonntApplication;
import son.nt.util.FileUtil;
import son.nt.util.GoogleAnaTool;
import son.nt.util.LogUtil;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class FileActivity extends Activity{
	private Context mContext = null;
	private static final String TAG = "FileActivity";
	private TextView txtPath = null;
	private String fTxt = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act2_file);
		mContext = this;
		txtPath = (TextView) findViewById(R.id.txtFolderPath);
		GoogleAnaTool.track("/" + TAG);
	}
	
	public void btnOnClick(View v){
		switch (v.getId()) {
		case R.id.btnTrialSave:
			Log.v(TAG, "btn Trial save");
			String path = FileUtil.getExternalPackagePath(mContext);
			LogUtil.v(mContext, TAG, "path is: " + path);
			File contentPath = new File(path, "h1");
			if(!contentPath.exists()){
				contentPath.mkdirs();
			}
			txtPath.setText(contentPath.getPath());
			fTxt = new File(contentPath, "son.mp3").getPath();
			
			break;
		case R.id.btnTrialDelete:
			getMusic();
			break;
		case R.id.btnFileCheckSDcard:
			if(FileUtil.isExternalSD()){
				LogUtil.toast(mContext, "have sd card");
			}else{
				LogUtil.toast(mContext, "no sd card");
			}
		case R.id.btnGetTitleSong:
			txtPath.setText(SonntApplication.getinstance().getTitleSong());
			break;
		default:
			break;
		}
	}
	
	public void getMusic(){
		try {
			URL url = new URL(Contains.URL);
			
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setRequestProperty("User-Agent", Contains.USER_AGENT);
			if(200 != con.getResponseCode()){
				LogUtil.toast(mContext, "Can't get music");
				return;
			}
			LogUtil.toast(mContext, "Downloading...");
			int fileSize = con.getContentLength();
			
			BufferedInputStream in = new BufferedInputStream(con.getInputStream());
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fTxt, false));
			byte buf[] = new byte[1024];
			int len;
			while((len = in.read(buf))!= -1){
				out.write(buf, 0, len);
			}
			out.close();
			in.close();
			LogUtil.v(mContext, TAG, "file size:" + fileSize);
			
			
		} catch (Exception e) {
			LogUtil.e(mContext, TAG, e.toString());
			e.printStackTrace();
		}
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
			txtPath.setText("ORIENTATION_LANDSCAPE");
		}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
			txtPath.setText("ORIENTATION_PORTRAIT");
		}
		
	}
}
